﻿Imports System.Windows.Forms

Public Class ApplyAllocationDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub ApplyAllocationDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPeriodBox()
        LoadFactorBox()

    End Sub

    Private Sub LoadPeriodBox()
        Dim sql As String

        sql = "Select * from Periods"

        PeriodBox.DataSource = LoadDataView(sql)
        PeriodBox.ValueMember = "Period_ID"
        PeriodBox.DisplayMember = "Period_Name"

        PeriodBox.SelectedValue = CurrentPeriodID
    End Sub

    Private Sub LoadFactorBox()
        Dim sql As String

        sql = "select '0' as datacut_id, '<None>' as description union " & _
            "select distinct datacut_id, description from Modelling_Factors " & _
            "where source not like 'Case%' " & _
            "order by description"

        FactorBox.DataSource = LoadDataView(sql)
        FactorBox.ValueMember = "datacut_id"
        FactorBox.DisplayMember = "description"
    End Sub

    Private Sub LoadCaseBox()
        Dim sql As String

        If FactorBox.SelectedValue.ToString = "0" Then
            sql = "select 'none' as DataCut_ID, '<None>' as Description"
        Else
            sql = "select 'none' as DataCut_ID, '<None>' as Description union " & _
            "select a.DataCut_ID, Description from " & _
            "(select DataCut_ID from dbo.Case_Templates " & _
            "where datacut_id_act = '" & FactorBox.SelectedValue.ToString & "' " & _
            "and Table_Name = 'Modelling_Factors') a, " & _
            "Case_Templates_IDs b  " & _
            "where a.DataCut_id = b.DataCut_Id " & _
            "order by Description"
        End If

        CaseBox.DataSource = LoadDataView(sql)
        CaseBox.ValueMember = "DataCut_ID"
        CaseBox.DisplayMember = "Description"
    End Sub

    Private Sub FactorBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FactorBox.SelectedIndexChanged
        LoadCaseBox()

    End Sub

    Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RateBox.Leave
        Dim anum As Double

        Try
            anum = RateBox.Text
        Catch ex As Exception
            MessageBox.Show("You have to enter a number, you can enter 0 if you dont want to apply a rate", "Enter a rate", MessageBoxButtons.OK, MessageBoxIcon.Error)
            RateBox.Focus()
        End Try
    End Sub
End Class
